/*	$Id: ctx.c 6 2008-07-29 02:30:10Z phrakt $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Packet Forge contexts are a small primitive to keep track of the source
 * of the executing code.  A context is implemented simply as an integer whose
 * value indicates if the code that is being executed is from the Packet
 * Forge library or from a module.  Any positive value is taken to be the
 * ID of the module owning the code; the special values PF_CTX_CORE and
 * PF_CTX_PROG can be used to set the context to the library itself and to
 * the program which is using the library, respectively.
 *
 * The use of contexts allows the functions in the library to tell if they
 * are being called from inside or from a module.  To work correctly,
 * functions calling handlers registered by modules should always set the
 * context correctly before entering the handler, and reset the previous
 * context after the handler has done executing.
 *
 * Example:
 *
 * void
 * execute_some_handler(struct handler_data *data)
 * {
 *	pf_ctx_t prev_ctx;
 *
 *	pf_ctx_set(data->ctx, &prev_ctx);
 *	data->hdlr(arg1, ...);
 *	pf_ctx_set(prev_ctx, NULL);
 * }
 *
 * When compiled without thread support, the context is kept in the
 * variable <pforge_ctx>, otherwise it is maintained on a per-thread basis
 * within each thread's specific data.
 */

#include <sys/types.h>

#include <unistd.h>

#include "private.h"

#ifdef PF_THREAD_SAFE
static pthread_key_t  pf_ctx_key;
#else
static pf_ctx_t pforge_ctx = PF_CTX_CORE;
#endif


/*
 * pf_ctx_init()
 *
 * Initialize support for execution context management.
 */
int
pf_ctx_init(void)
{
#ifdef PF_THREAD_SAFE
	if (pthread_key_create(&pf_ctx_key, NULL) != 0) {
		return (-1);
	}
#endif

	return (0);
}

/*
 * pf_ctx_set()
 *
 * Set the current execution context to <ctx> and save the previous context
 * in <old> if the pointer is not NULL.
 */
void
pf_ctx_set(pf_ctx_t ctx, pf_ctx_t *old)
{
#ifdef PF_THREAD_SAFE
	if (old != NULL)
		*old = (pf_ctx_t)pthread_getspecific(pf_ctx_key);
	pthread_setspecific(pf_ctx_key, (void *)ctx);
#else
	if (old != NULL)
		*old = pforge_ctx;
	pforge_ctx = ctx;
#endif
}


/*
 * pf_ctx_get()
 *
 * Retrieve the current execution context in <ctx>.
 */
void
pf_ctx_get(pf_ctx_t *ctx)
{
#ifdef PF_THREAD_SAFE
	*ctx = (pf_ctx_t)pthread_getspecific(pf_ctx_key);
#else
	*ctx = pforge_ctx;
#endif
}
